package com.hzy.servlet;

import com.hzy.pojo.Fruit;
import com.hzy.service.FruitService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Package: servlet
 * @ClassName: UpdateServlet
 * @Author: 862982795
 * @CreateTime: 2021/06/07 9:46
 * @Description:
 */
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        Fruit f = new Fruit();
        FruitService service = new FruitService();
        String type = (req.getParameter("type"));
        String breed = (req.getParameter("breed"));
        String area = (req.getParameter("area"));
        String brief = (req.getParameter("brief"));
        int weight = Integer.parseInt(req.getParameter("weight"));
        double price = Double.parseDouble(req.getParameter("price"));

        f.setType(type);
        f.setBreed(breed);
        f.setArea(area);
        f.setBrief(brief);
        f.setWeight(weight);
        f.setPrice(price);
        f.setFruitId((int) (req.getSession().getAttribute("fruitId")));

        int i = service.add(f);
        PrintWriter out = resp.getWriter();
        if (i > 0) {
            out.println("<script>alert('修改成功!');location.href='ListServlet';</script>");
        } else {
            out.println("<script>alert('修改失败!');history.back();</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
